package com.example.himalaya.fragments;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.DetailActivity;
import com.example.himalaya.R;
import com.example.himalaya.adapters.AlbumListRvAdapter;
import com.example.himalaya.adapters.AlbumListRvCardAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.interfaces.IRecommendViewCallBack;
import com.example.himalaya.presenters.AlbumDetailPresenter;
import com.example.himalaya.presenters.RecommendPresenter;
import com.example.himalaya.views.UILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.List;

/**
 * 推荐Fragment片段
 * 并且实现推荐页面UI逻辑接口，V接口实现类
 */
public class RecommendFragment extends BaseFragment implements IRecommendViewCallBack {
    private static final String TAG = "RecommendFragment";
    private View mSuccessView;
    private RecyclerView mRecommendRv;
    private RecommendPresenter mRecommendPresenter;
    private UILoader mUILoader;
    private AlbumListRvCardAdapter mAlbumListRvCardAdapter;

    @Override
    protected View onSubViewLoaded(LayoutInflater inflater, ViewGroup container) {

        initView(inflater, container);

        initPresenter();

        intEvent();

        //返回UI加载器中的view视图界面给父类Fragment片段
        return mUILoader;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        //创建UI加载器，并创建成功视图界面
        mUILoader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(inflater,container);
            }
        };
        //android控件不允许多次绑定父控件，需要在之前的FrameLayout父控件中进行移除解绑mUILoader的操作，才能提交到本类的Fragment
        if (mUILoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUILoader.getParent()).removeView(mUILoader);
        }

    }

    private void initPresenter() {
        //获取推荐业务逻辑对象
        mRecommendPresenter = RecommendPresenter.getRecommendPresenter();
        //推荐业务逻辑对象注册推荐UI逻辑对象，由本类实现接口并给予提供
        mRecommendPresenter.registerViewCallBack(this);
        //获取推荐专辑列表
        mRecommendPresenter.getRecommendAlbumList();
    }

    private void intEvent() {
        //设置重试点击监听器，重新获取数据
        mUILoader.setOnRetryClickListener(new UILoader.OnRetryClickListener() {
            @Override
            public void onRetryClick() {
                if (mRecommendPresenter != null) {
                    //重新获取数据即可
                    mRecommendPresenter.getRecommendAlbumList();
                }
            }
        });

        //设置Item点击监听器
        mAlbumListRvCardAdapter.setOnItemClickListener(new AlbumListRvCardAdapter.onItemClickListener() {
            @Override
            public void OnItemClick(int position, Album album) {
                AlbumDetailPresenter albumDetailPresenter = AlbumDetailPresenter.getAlbumDetailPresenter();
                albumDetailPresenter.setTargetAlbum(album);

                //跳转到专辑详情界面
                Intent intent = new Intent(getContext(), DetailActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 创建UILoader中要加载的成功界面
     * @return
     * @param inflater
     * @param container
     */
    private View createSuccessView(LayoutInflater inflater, ViewGroup container) {
        //view加载完成
        mSuccessView = inflater.inflate(R.layout.fragment_recommend, container, false);
        //RecyclerView
        //1.找到控件
        mRecommendRv = mSuccessView.findViewById(R.id.recommended_list_Rv);
        //2.设置布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        mRecommendRv.setLayoutManager(gridLayoutManager);
        //3.设置适配器
        mAlbumListRvCardAdapter = new AlbumListRvCardAdapter();
        mRecommendRv.setAdapter(mAlbumListRvCardAdapter);

        TwinklingRefreshLayout refreshLayout = mSuccessView.findViewById(R.id.recommend_refreshLayout);
        refreshLayout.setPureScrollModeOn(true);

        return mSuccessView;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mRecommendPresenter != null) {
            //取消推荐UI逻辑对象，避免内存泄漏
            //东西已经不用了，还有人持有它的引用，此处推荐业务逻辑对象持有推荐UI逻辑对象。
            // 由于本类实现了推荐视图逻辑接口并注册，所以推荐业务逻辑对象持有的其实是推荐Fragment片段
            mRecommendPresenter.unRegisterViewCallBack(this);
            mRecommendPresenter = null;
        }
    }



    //-------------------IRecommendViewCallBack---------------------
    @Override
    public void onLoadRecommendAlbumList(List<Album> albums) {
        //获取推荐专辑列表成功，调用该方法。
        mAlbumListRvCardAdapter.setData(albums);
        mUILoader.updateUIStatus(UILoader.UIStatus.SUCCESS);
    }

    @Override
    public void onLoadLoading() {
        mUILoader.updateUIStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onLoadNetworkError() {
        mUILoader.updateUIStatus(UILoader.UIStatus.NETWORK_ERROR);

    }

    @Override
    public void onLoadDataEmpty() {
        mUILoader.updateUIStatus(UILoader.UIStatus.EMPTY);

    }

}
